package com.df.excel_export.validator.vadator_impl.type_float;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;
import com.df.excel_export.validator.vadator_impl.type_int.IntEpsValidator;
import com.df.excel_export.validator.vadator_impl.type_int.IntFkValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FloatEpsValidator extends AbstractIFieldValidator implements IValidatorFactory {
    public Set<String> otherFieldName = new HashSet<>();
    public String expres;//表达式
    private static Logger log = LoggerFactory.getLogger(IntFkValidator.class.getName());

    @Override
    public boolean support(String filedType, String ruleStr) {
        if(!filedType.equals("float")){
            return false;
        }
        ruleStr  = ruleStr.trim();
        if(ruleStr.startsWith("eps(") && ruleStr.endsWith(")")){
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        IntEpsValidator validator = new IntEpsValidator();
        String trim = ruleStr.trim();
        String eps = trim.substring(4, trim.length() - 1);
        int startIndex = -1;
        char[] charArray = eps.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if(c == '{'){
                startIndex = i+1;
                continue;
            }
            if(c=='}'){
//                if(startIndex==-1 || startIndex==i){//表达式不正确
//
//                    break;
//                }
                //{}内的内容为其他列字段名
                String fieldName = trim.substring(startIndex, i - 1);
                otherFieldName.add(fieldName);
                startIndex=-1;
                continue;
            }
        }
        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }


    @Override
    public boolean validate(ExcelTable checkTable, List<Object> rowData, Integer dataDowIndex, Integer fieldIndex, String fieldNameString , String fieldTypeStr, Object filedValue) {
        List<String> fieldNames = checkTable.getFieldNames();
        ExpressionParser parser = new SpelExpressionParser();
        String finalEps = "";
        for (String fieldName : otherFieldName) {
            int i = fieldNames.indexOf(fieldName);
            Object otherValue = rowData.get(i);
            finalEps = expres.replace("{"+fieldName+"}", otherValue.toString());
        }
        Object value = parser.parseExpression(finalEps).getValue();
        if(!filedValue.equals(value)){
            return false;
        }
        return true;
    }
}
